var oss = require('ali-oss');
staticPath1="D:/G/gitee/ming_api_mock_plugins/node_oss_upload/src/images"


var client  = oss({
  accessKeyId: 'LTAIMre888eRk2Kk',
  accessKeySecret: 'ExMminglieo0OBy2UTfMXso6ArrWiAOyhKMN',
  bucket: 'ming-bucket-01',
  region: 'oss-cn-beijing'
});


async function list () {
    var result =await  store.listBuckets({
        "max-keys": 1
      });
      console.log(result);
}
  




async function put () {
    try {
         //object-name可以自定义为文件名（例如file.txt）或目录（例如abc/test/file.txt）的形式，实现将文件上传至当前Bucket或Bucket下的指定目录。
      let result = await client.put('test0608001', 'D:/G/gitee/ming_api_mock_plugins/node_oss_upload/src/images/a.txt');
      console.log(result);
     } catch (err) {
       console.log (err);
     }
}
  

async function putPbject(key,filePath) {
  try {
    let result = await client.put(key,filePath);
    console.log(key);
   } catch (err) {
     console.log (err);
   }
}



var fs = require('fs')

function geFileList(path)
{
   var filesList = [];
   var targetObj = {};
   readFile(path,filesList,targetObj);
   return filesList;
}

//遍历读取文件
function readFile(path,filesList,targetObj)
{
   files = fs.readdirSync(path);//需要用到同步读取
   files.forEach(walk);
   function walk(file)
   {  
        states = fs.statSync(path+'/'+file);         
        if(states.isDirectory())
        {
            var item ;
            if(targetObj["children"])
            {
                item ={name:file,children:[],value:path+'/'+file};
                targetObj["children"].push(item);
            }
            else
            {
               item = {name:file,children:[],value:path+'/'+file};
               filesList.push(item);
            }

            readFile(path+'/'+file,filesList,item);
        }
        else
        {   
            //创建一个对象保存信息
            var obj = new Object();
            obj.size = states.size;//文件大小，以字节为单位
            obj.name = file;//文件名
            obj.path = path+'/'+file; //文件绝对路径

            if(targetObj["children"])
            {
               var item = {name:file,value:obj.path}
               targetObj["children"].push(item);
            }
            else
            {
                var item = {name:file,value:obj.path};
                filesList.push(item);
            }
        }     
    }
}









+async function(){
        //let r = await put();
        //console.log(r)
        let i=1;
        var filesList = geFileList(staticPath1);
        console.log("total:"+filesList.length)
        filesList.forEach(async u=>{
              await putPbject("yuque/ming_api_mock/"+u.name,u.value);
              console.log(i++)
        })
       //console.log(filesList)

}();


